package com.framework.admin.schedule.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.admin.schedule.constant.Constants;
import com.framework.admin.schedule.entity.SysJob;
import com.framework.admin.schedule.param.JobParam;
import com.framework.admin.schedule.service.IJobService;
import com.framework.admin.schedule.util.CronUtils;
import com.framework.admin.schedule.util.WhiteListUtils;
import com.framework.common.domain.ResultData;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/job")
public class JobController {
    @Autowired
    private IJobService jobService;

    @ApiOperation("获取所有的任务")
    @PostMapping(value = "/list")
    public ResultData listJobs(@RequestBody JobParam param) {
        Page request = Page.of(param.getPageNo(), param.getPageSize());
        Page<SysJob> users = jobService.listJobs(param, request);
        return ResultData.data(users);
    }

    @ApiOperation("新增任务")
    @PostMapping(value = "/add")
    public ResultData addJob(@RequestBody SysJob job) {
        if (!CronUtils.isValid(job.getCronExpression())) {
            return ResultData.error("新增任务'" + job.getJobName() + "'失败，Cron表达式不正确");
        } else if (WhiteListUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.HTTP, Constants.HTTPS})) {
            return ResultData.error("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'http(s)'调用");
        } else if (WhiteListUtils.containsAnyIgnoreCase(job.getInvokeTarget(), Constants.JOB_ERROR_STR)) {
            return ResultData.error("新增任务'" + job.getJobName() + "'失败，目标字符串存在违规");
        }
        jobService.addJob(job);
        return ResultData.success();
    }

    @ApiOperation("获取单个任务")
    @PostMapping(value = "/{id}")
    public ResultData getJob(@PathVariable Long id) {
        SysJob job = jobService.getJob(id);
        return ResultData.data(job);
    }

    @ApiOperation("编辑任务")
    @PostMapping(value = "/edit")
    public ResultData updateJob(@RequestBody SysJob job) {
        jobService.updateJob(job);
        return ResultData.success();
    }

    @ApiOperation("暂停任务")
    @PostMapping(value = "/status/{id}")
    public ResultData schedulerJob(@PathVariable Long id) throws SchedulerException {
        jobService.schedulerJob(id);
        return ResultData.success();
    }

    @ApiOperation("启动任务")
    @PostMapping(value = "/start/{id}")
    public ResultData startJob(@PathVariable Long id) {
        jobService.startJob(id);
        return ResultData.success();
    }

    @ApiOperation("删除任务")
    @PostMapping(value = "/delete/{id}")
    public ResultData deleteJob(@PathVariable Long id) {
        jobService.deleteJob(id);
        return ResultData.success();
    }

    /**
     * 定时任务立即执行一次
     */
    @PutMapping("/run")
    public ResultData run(@RequestBody SysJob job) {
        boolean result = jobService.run(job);
        return result ? ResultData.success() : ResultData.error("任务不存在或已过期！");
    }

}
